package com.idis.common.base;

import com.idis.common.dictionary.HttpCode;
import org.springframework.security.core.context.SecurityContextImpl;
import org.springframework.ui.Model;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Ming on 2018/1/26.
 */

/**
 * 基础控制器
 * 供控制器继承，以提供添加返回参数、返回信息的方法
 */
public class BaseController {

    /**
     * 适配DataTable前端框架，将数据包装进"data"列表
     *
     * {
     *     data:object
     * }
     * @param object
     * @return
     */
    protected Map sendDate(Object object){
        Map map=new HashMap();
        map.put("data",object);
        return map;
    }

    protected SecurityContextImpl securityContextImpl ;

    /**
     * 返回登录的相关信息
     * @param request
     * @return
     */
    protected SecurityContextImpl getSecurityContext(HttpServletRequest request){
        securityContextImpl=(SecurityContextImpl) request.getSession().getAttribute("SPRING_SECURITY_CONTEXT");
        return securityContextImpl;
    }

    /**
     * 返回当前登录用户的用户名
     * @param request
     * @return 若已登录，返回用户名，否则返回“未登录”
     */
    protected String getUsername(HttpServletRequest request){
        getSecurityContext(request);
        if(securityContextImpl!=null)
            return securityContextImpl.getAuthentication().getName();
        else
            return "未登录";
    }

    /**
     * 若前端表单未填写完整，返回信息提示
     * @param map
     */
    protected void sendNull(Map map){
        addResult(map,"0");
        addMessage(map,"请将信息填写完整.");
    }
    /**
     * 使用Model添加msg消息
     * @param messages
     */
    protected void addMessage(Model model, String... messages) {
        StringBuilder sb = new StringBuilder();
        for (String message : messages){
            sb.append(message).append(messages.length>1?"<br/>":"");
        }
        model.addAttribute("msg", sb.toString());
    }

    /**
     * 使用Map添加msg消息
     * @param map
     * @param messages
     */
    protected void addMessage(Map map, String... messages) {
        StringBuilder sb = new StringBuilder();
        for (String message : messages){
            sb.append(message).append(messages.length>1?"<br/>":"");
        }
        map.put("msg", sb.toString());
    }

    /**
     *使用Map添加result消息
     * @param map
     * @param result
     */
    protected void addResult(Map map,String... result){
        StringBuilder sb = new StringBuilder();
        for (String message : result){
            sb.append(message).append(result.length>1?"<br/>":"");
        }
        map.put("result", sb.toString());
    }

    /**
     * 使用Model添加result消息
     * @param model
     * @param code
     */
    protected void sendCode(Model model,HttpCode code){
        model.addAttribute("statusCode",code.getCode());
        model.addAttribute("statusMsg",code.getMessage());
    }

    /**
     * 使用Map添加result消息
     * @param map
     * @param code
     */
    protected void sendCode(Map map, HttpCode code){
        map.put("statusCode",code.getCode());
        map.put("statusMsg",code.getMessage());
    }

}
